/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *  2011-03-25 Marco Riccardi - implementation of visitor pattern
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.IElement;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLAttribute;
import org.mizartools.utility.xml.XMLElement;

public class Identify implements IElement {

    public enum Constrkind { K, U, G, V, R,}

    public static final String NAME = "Identify";

    private INode parentNode;
    private String aid = null;
    private Integer nr = null;
    private Constrkind constrkind = null;

    private ErrorIdentify errorIdentify = null;
    private LinkedList<Typ> typList = new LinkedList<Typ>();
    private Term term1 = null;
    private Term term2 = null;
    private Formula formula1 = null;
    private Formula formula2 = null;
    private EqArgs eqArgs = null;


    private Identify(INode parentNode){
        this.parentNode = parentNode;
    }

    public INode getParentNode() {
        return parentNode;
    }

    public static Identify newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        Identify identify = new Identify(parentNode);
        XMLElement xmlElement = xmlElementList.poll();
        if (!xmlElement.getName().equals(NAME)){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a " + NAME, xmlElementList);
        }
        for (XMLAttribute xmlAttribute : xmlElement.getXMLAttributeList()){
            if (xmlAttribute.getName().equals("aid")){
                if (identify.aid == null){
                    identify.aid = xmlAttribute.getValue();
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("nr")){
                if (identify.nr == null){
                    try {identify.nr = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
            if (xmlAttribute.getName().equals("constrkind")){
                if (identify.constrkind == null){
                    if (Constrkind.valueOf(xmlAttribute.getValue()) != null) {
                        identify.constrkind = Constrkind.valueOf(xmlAttribute.getValue());
                    } else 
                        throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
            } else 
                throw new ElementParseException(ElementParseException.NOT_VALID_ATTRIBUTE_ELEMENT, "XMLElement has not a valid attribute " + xmlAttribute.getName(), xmlElementList);
        }

        if (!xmlElement.isEmpty()){
            while (true){
                xmlElement = xmlElementList.peek();
                if (xmlElement == null){
                    throw new ElementParseException(ElementParseException.UNEXPECTED_END_OF_FILE, "Unexpected end of XMLElement list");
                }
                if (xmlElement.getName().equals(NAME) && xmlElement.isEndElement()){
                    xmlElement = xmlElementList.poll();
                    break;
                }
                if (xmlElement.getName().equals("ErrorIdentify")){
                    if (identify.errorIdentify == null) {
                        identify.errorIdentify = ErrorIdentify.newInstance(identify, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <ErrorIdentify> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("Typ")){
                    identify.typList.add(Typ.newInstance(identify, xmlElementList));
                } else if (Term.isTerm(xmlElement.getName()) && identify.term1 == null) {
                    identify.term1 = Term.newInstance(identify, xmlElementList);
                } else if (Term.isTerm(xmlElement.getName())){
                    if (identify.term2 == null) {
                        identify.term2 = Term.newInstance(identify, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Term> in element " + NAME, xmlElementList);
                    }
                } else if (Formula.isFormula(xmlElement.getName()) && identify.formula1 == null) {
                    identify.formula1 = Formula.newInstance(identify, xmlElementList);
                } else if (Formula.isFormula(xmlElement.getName())){
                    if (identify.formula2 == null) {
                        identify.formula2 = Formula.newInstance(identify, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Formula> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("EqArgs")){
                    if (identify.eqArgs == null) {
                        identify.eqArgs = EqArgs.newInstance(identify, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <EqArgs> in element " + NAME, xmlElementList);
                    }
                } else 
                    throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "The XMLElement " + xmlElement.getName() + " is not valid in " + NAME, xmlElementList);
            }
        }

        return identify;
    }

    public LinkedList<XMLElement> getXMLElementList() {
        LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
        XMLElement xmlElement = new XMLElement(NAME);
        if (this.aid != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("aid", this.aid);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.nr != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("nr", this.nr);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.constrkind != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("constrkind", this.constrkind.name());
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        boolean isEmpty = !(errorIdentify != null
         || !typList.isEmpty() || term1 != null
         || term2 != null || formula1 != null
         || formula2 != null || eqArgs != null);
        xmlElement.setEmpty(isEmpty);
        xmlElementList.add(xmlElement);
        if (this.errorIdentify != null){
            xmlElementList.addAll(this.errorIdentify.getXMLElementList());
        }
        for (Typ typ : this.typList){
            xmlElementList.addAll(typ.getXMLElementList());
        }
        if (this.term1 != null){
            xmlElementList.addAll(this.term1.getXMLElementList());
        }
        if (this.term2 != null){
            xmlElementList.addAll(this.term2.getXMLElementList());
        }
        if (this.formula1 != null){
            xmlElementList.addAll(this.formula1.getXMLElementList());
        }
        if (this.formula2 != null){
            xmlElementList.addAll(this.formula2.getXMLElementList());
        }
        if (this.eqArgs != null){
            xmlElementList.addAll(this.eqArgs.getXMLElementList());
        }
        if (!isEmpty) { 
            xmlElement = new XMLElement(NAME);
            xmlElement.setEndElement(true);
            xmlElementList.add(xmlElement);
        }
        return xmlElementList;
    }

    public String getAid(){
        return aid;
    }

    public Integer getNr(){
        return nr;
    }

    public Constrkind getConstrkind(){
        return constrkind;
    }

    public ErrorIdentify getErrorIdentify(){
        return errorIdentify;
    }

    public LinkedList<Typ> getTypList(){
        return typList;
    }

    public Term getTerm1(){
        return term1;
    }

    public Term getTerm2(){
        return term2;
    }

    public Formula getFormula1(){
        return formula1;
    }

    public Formula getFormula2(){
        return formula2;
    }

    public EqArgs getEqArgs(){
        return eqArgs;
    }

	@Override
	public void accept(Visitor visitor) {
		if (visitor.visit(this)) {
			for(Typ typ: this.typList) {
				typ.accept(visitor);
			}
		}
		visitor.endVisit(this);
	}

	@Override
	public String toString(){
		ToStringVisitor toStringVisitor = new ToStringVisitor();
		this.accept(toStringVisitor);
		return toStringVisitor.toString();
	}

}
